package com.aptech.yuanhuabao.ui.start.model;

import com.aptech.yuanhuabao.api.ApiService;
import com.aptech.yuanhuabao.api.ApiXml;
import com.aptech.yuanhuabao.api.HostType;
import com.aptech.yuanhuabao.api.soap.request.LoginRequestBody;
import com.aptech.yuanhuabao.api.soap.request.LoginRequestData;
import com.aptech.yuanhuabao.api.soap.request.LoginRequestEnvelope;
import com.aptech.yuanhuabao.api.soap.response.LoginResponseEnvelope;
import com.aptech.yuanhuabao.api.soap.result.LoginResult;
import com.aptech.yuanhuabao.ui.start.contract.LoginContract;
import com.jaydenxiao.common.baserx.RxSchedulers;

import rx.Observable;
import rx.functions.Func1;

/**
 * des:
 * Created by zhh_li
 * on 2017/1/5.
 */

public class LoginModel implements LoginContract.Model {
    private static final String LOG_TAG = "LoginModel";

    @Override
    public Observable<LoginResult> checkLogin(String phone, String sign) {

        LoginRequestData loginInfoRequest = new LoginRequestData(phone, sign);
        LoginRequestBody loginRequestBody = new LoginRequestBody(loginInfoRequest);
        LoginRequestEnvelope requestEnvelop = new LoginRequestEnvelope(loginRequestBody);


        ApiService apiService = ApiXml.getDefault(HostType.IPC_CENTER);

        Observable<LoginResult> observable = apiService.login(requestEnvelop)
                .map(new Func1<LoginResponseEnvelope, LoginResult>() {
                    @Override
                    public LoginResult call(LoginResponseEnvelope loginResponseEnvelope) {
                        String str = loginResponseEnvelope.getBody().getLoginResponseAction3().getLoginResponseReturn();
                        LoginResult loginResult = new LoginResult(str);

                        // LogUtils.logd(LOG_TAG, loginResult.toString());
                        return loginResult;
                    }
                })
                .compose(RxSchedulers.<LoginResult>io_main());


        return observable;
    }
}
